import { uuid } from '../utils/uuid';
import { positionRepository, PositionEntity } from '../utils/repository';
import { Service } from 'typedi';

@Service()
export class PositionService {
  /**
   * 获取职位列表
   */
  async list(condition: any) {
    return await positionRepository.findAndCount(condition);
  }

  /**
   * 添加/更新职位
   * @param eid 企业id
   * @param entity
   */
  async save(eid: string, entity: PositionEntity) {
    const result = await positionRepository.findOneBy({
      name: entity.name,
    });
    // 如果存在此条记录，进行更新
    if (result) {
      const { affected } = await positionRepository.update(result.id, entity);
      return affected;
    }
    entity.pid = uuid.simpleUUID();
    entity.eid = eid;
    const { raw } = await positionRepository.insert(entity);
    return raw.affectedRows;
  }
}
